﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using coffeemaker.contracts;

namespace coffeemaker.simulation
{
    public class ErraticDummySimulation : ICoffeeMakerSimulation
    {
        private Timer _simulationLoop;


        public void Start()
        {
            Trace.TraceInformation("SupersimpleSimulation.Start");
            _simulationLoop = new Timer(Simulate, null, 0, 500);
        }

        public void Pause()
        {
            Trace.TraceInformation("SupersimpleSimulation.Pause");
            this.Stop();
        }

        public void Stop()
        {
            Trace.TraceInformation("SupersimpleSimulation.Stop");
            if (_simulationLoop == null) return;
            _simulationLoop.Change(Timeout.Infinite, Timeout.Infinite);
        }


        public void SwitchBoiler(bool on)
        {
            Trace.TraceInformation("SupersimpleSimulation.SwitchBoiler: {0}", on);
        }

        public void FillBoiler()
        {
            Trace.TraceInformation("SupersimpleSimulation.FillBoiler");
        }

        public void SwitchWarmer(bool on)
        {
            Trace.TraceInformation("SupersimpleSimulation.SwitchWarmer: {0}", on);
        }

        public void PlacePot(bool onWarmer)
        {
            Trace.TraceInformation("SupersimpleSimulation.PlacePot: {0}", onWarmer);
        }

        public void EmptyPot()
        {
            Trace.TraceInformation("SupersimpleSimulation.EmptyPot");
        }

        public void PressBrewButton()
        {
            Trace.TraceInformation("SupersimpleSimulation.PressBrewButton");
        }

        public void SwitchValve(bool open)
        {
            Trace.TraceInformation("SupersimpleSimulation.SwitchValve: {0}", open);
        }

        public void Dispose()
        {
            Trace.TraceInformation("SupersimpleSimulation.Dispose");
            this.Stop();
        }


        public event Action<CoffeeMakerState> OnStateChange;


        private void Simulate(object _)
        {
            var s = new CoffeeMakerState();
            s.boilerPercentFilled = (DateTime.Now.Second*2)%100;
            s.potPercentFilled = 100 - s.boilerPercentFilled;

            s.boilerOn = DateTime.Now.Second % 3 == 0; ;
            s.boilerOverheated = DateTime.Now.Second % 10 == 0; ;
            s.brewButtonPushed = DateTime.Now.Second % 3 == 0; ;
            s.indicatorOn = DateTime.Now.Second%2 == 0;
            s.potOnWarmer = DateTime.Now.Second % 5 == 0; ;
            s.steamBlowingOffThroughOpenValve = false;
            s.valveOpen = DateTime.Now.Second % 4 == 0; ;
            s.warmerOn = DateTime.Now.Second % 3 == 0; ;
            s.warmerOverheated = DateTime.Now.Second % 11 == 0; ;
            s.waterSpillingOnEmptyWarmer = DateTime.Now.Second % 15 == 0; ;

            this.OnStateChange(s);
        }
    }
}
